//
//  ChildPlanDetailsViewController.m
//  FitTime
//
//  Created by dllo on 16/4/5.
//  Copyright © 2016年 赵建平. All rights reserved.
//

#import "ChildPlanDetailsViewController.h"
#import "SingleModel.h"
#import "ChildPlanDetailTableViewCell.h"

@interface ChildPlanDetailsViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *singleModelArray;


@end

@implementation ChildPlanDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self getData];
}



- (void)setIDArray:(NSMutableArray *)IDArray {
    _IDArray = IDArray;
    [self.tableView reloadData];
}

#pragma mark - 创建tableView
- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 170 - 64)];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.bounces = NO;
    
    [self.tableView registerClass:[ChildPlanDetailTableViewCell class] forCellReuseIdentifier:@"ChildPlanDetailTableViewCellIdentifier"];
    
    [self.view addSubview:self.tableView];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.singleModelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ChildPlanDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChildPlanDetailTableViewCellIdentifier"];
    
    SingleModel *singleModel = self.singleModelArray[indexPath.row];
    cell.singleModel = singleModel;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return SCREENHEIGHT / 5;
}



#pragma mark - 数据解析
- (void)getData {
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [ZJPAFNetTool GETNetWithUrl:@"http://api.fit-time.cn/ftvideo/getAllVideos" body:nil headFile:nil response:ZJPJSON success:^(id result) {
        self.singleModelArray = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dic in result[@"videos"]) {
            SingleModel *model = [[SingleModel alloc] initWithDic:dic];
            for (int i = 0; i < self.IDArray.count; i++) {
                if (model.ID == self.IDArray[i]) {
                    [self.singleModelArray addObject:model];
                }
            }
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [hud hide:YES];
            [self.tableView reloadData];
        });
        [self createTableView];
    } failure:^(NSError *error) {
        
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
